<?php

namespace Product\Controller;
use Common\Controller\HomebaseController; 
/**
 * 会员中心
 */
class MemberController extends HomebaseController {
	
    //会员中心
	public function index() {
        $userid=sp_get_current_userid();

        $users = D("Common/Users");
        $flag = $users->where(array('id'=>$userid))->find();
        $this->assign("user",$flag);
		$this->assign("open_menu",false);
    	$this->display();
    }

    /**
     *  修改头像
     */
    public function modify_avatar(){
        $this->assign("menu_name",'user_info');
        $this->assign("title",'修改头像');
        $this->display();
    }

    /**
     *  修改头像上传
     */
    public function modify_avatar_upload(){
        $adminid=sp_get_current_admin_id();
        $userid=sp_get_current_userid();
        if(empty($adminid) && empty($userid)){
            exit("非法上传！");
        }
    
        // 文件上传
        $upload_setting=sp_get_upload_setting();
        
        $filetypes=array(
            'image'=>array('title'=>'Image files','extensions'=>$upload_setting['image']['extensions']),
            'video'=>array('title'=>'Video files','extensions'=>$upload_setting['video']['extensions']),
            'audio'=>array('title'=>'Audio files','extensions'=>$upload_setting['audio']['extensions']),
            'file'=>array('title'=>'Custom files','extensions'=>$upload_setting['file']['extensions'])
        );
        
        $image_extensions=explode(',', $upload_setting['image']['extensions']);
        
        if (IS_POST) {
            $all_allowed_exts=array();
            foreach ($filetypes as $mfiletype){
                array_push($all_allowed_exts, $mfiletype['extensions']);
            }
            $all_allowed_exts=implode(',', $all_allowed_exts);
            $all_allowed_exts=explode(',', $all_allowed_exts);
            $all_allowed_exts=array_unique($all_allowed_exts);
            
            $file_extension=sp_get_file_extension($_FILES['file']['name']);
            $upload_max_filesize=$upload_setting['upload_max_filesize'][$file_extension];
            $upload_max_filesize=empty($upload_max_filesize)?2097152:$upload_max_filesize;//默认2M
            
            $app=I('post.app/s','');
            if(!in_array($app, C('MODULE_ALLOW_LIST'))){
                $app='default';
            }else{
                $app= strtolower($app);
            }
            
            $savepath=$app.'/'.date('Ymd').'/';
            //上传处理类
            $config=array(
                    'rootPath' => './'.C("UPLOADPATH"),
                    'savePath' => $savepath,
                    'maxSize' => $upload_max_filesize,
                    'saveName'   =>    array('uniqid',''),
                    'exts'       =>    $all_allowed_exts,
                    'autoSub'    =>    false,
            );
            $upload = new \Think\Upload($config);// 
            $info=$upload->upload();
            
            $file_path = $_SERVER['DOCUMENT_ROOT'].'/'.C("UPLOADPATH").$info['imageFile']['savepath'];
            $read_path = '/'.C("UPLOADPATH").$info['imageFile']['savepath'];
            $filename = $info['imageFile']['savename'];
            $return_path = $file_path.$filename;
            $thumb = true;
            $thumb_width = 300;
            $thumb_height = 300;
            $thumb_prefix = 'thumb_';

            $img = new \Think\Image();//实例化
            $img->open($file_path.$filename);//打开物理图片

            $img->thumb($thumb_width, $thumb_height,\Think\Image::IMAGE_THUMB_FIXED)->save($file_path.$thumb_prefix.$filename);

            $return_path = $read_path.$thumb_prefix.$filename;

            $this->assign("return_path",$return_path);
        } else {
            // 跳转回去
            $this->error("上传失败",U("Member/modify_avatar"));
        }

        $this->assign("title",'修改头像');
        $this->display();
    }

    /**
     *  修改头像
     */
    public function modify_avatar_save(){
        $userid=sp_get_current_userid();
        $avatar=I("post.avatar");

        $users = D("Common/Users");
        $flag = $users->where(array('id'=>$userid))->save(array('avatar'=>$avatar));
        $this->ajaxReturn(array('status'=>1,'message'=>'success'));
    }

    /**
     * 查询订单
     */
    public function select_order(){
        $this->assign("menu_name",'select_order');
        $this->assign("title",'订单列表');
        $this->display();
    }

    /**
     * 团购订单
     */
    public function group_order(){
        $this->assign("menu_name",'group_order');
        $this->assign("title",'团购订单');
        $this->display();
    }

    /**
     * 退换货
     */
    public function refund(){
        $this->assign("menu_name",'refund');
        $this->assign("title",'退换货记录');
        $this->display();
    }

    /**
     * 申请体现
     */
    public function cash(){
        $this->assign("menu_name",'cash');
        $this->assign("title",'退货订单提现申请');
        $this->display();
    }

    /**
     * 现金体现记录
     */
    public function cash_record(){
        $this->assign("menu_name",'cash_record');
        $this->assign("title",'现金提现记录');
        $this->display();
    }

    /**
     * 账户余额
     */
    public function balance(){
        $userid=sp_get_current_userid();

        $users = D("Common/Users");
        $flag = $users->where(array('id'=>$userid))->find();
        $this->assign("user",$flag);

        $this->assign("menu_name",'balance');
        $this->assign("title",'账户余额');
        $this->display();
    }

    /**
     * 我的积分
     */
    public function integral(){
        $userid=sp_get_current_userid();

        $users = D("Common/Users");
        $flag = $users->where(array('id'=>$userid))->find();
        $this->assign("user",$flag);
        $this->assign("menu_name",'integral');
        $this->assign("title",'我的积分');
        $this->display();
    }

    /**
     * 购物券
     */
    public function coupon(){
        $this->assign("menu_name",'coupon');
        $this->assign("title",'购物券');
        $this->display();
    }

    /**
     * 个人资料
     */
    public function user_info(){
        $this->assign("menu_name",'user_info');
        $this->assign("title",'个人资料');
        $session_user=session('user');
        $user = M("Users")->where(array('id'=>$session_user['id']))->find();
        $this->assign("user",$user);
        $this->display();
    }
    /**
     * 修改密码
     */
    public function modify_pwd(){
        $session_user=session('user');
        $user = M("Users")->where(array('id'=>$session_user['id']))->find();
        $this->assign("user",$user);
        $this->display();
    }

    public function get_old_pwd(){
        $userPsw = I('post.userPsw');
        $session_user=session('user');
        $user = M("Users")->where(array('id'=>$session_user['id']))->find();
        if($user['user_pass'] == sp_password($userPsw)){
            die(json_encode(array('success'=>true)));
        }else{
            die(json_encode(array('success'=>false)));
        }
    }

    /**
     * 保存密码
     */
    public function save_pwd(){
        $userPsw = I('post.userPsw');
        $session_user=session('user');
        $result = M("Users")->where(array('id'=>$session_user['id']))->save(array('user_pass'=>sp_password($userPsw)));
        if($result){
            die(json_encode(array('success'=>true,'errorCode'=>0)));
        }else{
            die(json_encode(array('success'=>true,'errorCode'=>-1)));
        }
    }

    /**
     * 保存用户资料
     */
    public function save_user(){
        $userName = I("post.userName"); //用户名
        $userMobile = I("post.userMobile"); //用户名
        $userEmail = I("post.userEmail"); //用户名
        $session_user=session('user');
        $user_result =  M("Users")->where(array("id"=>$session_user['id']))->save(array('user_nicename'=>$userName,'mobile'=>$userMobile,'user_email'=>$userEmail));
       if($user_result){
           die(json_encode(array('success'=>true)));
       }else{
           die(json_encode(array('success'=>false)));
       }
    }

    /**
     * 收获地址
     */
    public function address(){
        $m_address = M('Address');
        $session_user=session('user');
        $list = $m_address->where(array('uid'=>$session_user['id']))->select();
        $this->assign("list",$list);
        $this->assign("menu_name",'address');
        $this->assign("title",'收获地址');
        $this->display();
    }

    /**
     * 收藏商品
     */
    public function collection(){
        $this->assign("menu_name",'collection');
        $this->assign("title",'收藏商品');
        $this->display();
    }

    /**
     * 邀请好友
     */
    public function invitation(){
        $userid = sp_get_current_userid();
        $m_user = M('Users');
        $user = $m_user->where(array('id'=>$userid))->find();
        $this->assign('user',$user);
        $this->assign("menu_name",'invitation');
        $this->assign("title",'邀请好友');
        $this->display();
    }

    public function invite_send_email(){

        $send_result=sp_send_email(I('post.email'), I('post.title'), I('post.content'));

        if($send_result['error']){
            $this->error('邀请邮件发送失败！');
        }else{
            $this->success('发送成功！');
        }
    }

    /**
     * 投诉留言
     */
    public function complaint(){
        $this->assign("menu_name",'complaint');
        $this->assign("title",'投诉留言');
        
        $userid = sp_get_current_userid();
        $m_users = M("Users");
        $m_complaint = M("Complaint");

        if (IS_POST) {
            $content=I('post.content/s','');

            $save_data = array(
                'userid'=>$userid,
                'content'=>$content,
                'type'=>1,
            );
            $return = $m_complaint->add($save_data);
            $this->ajaxReturn(array('status'=>1,'message'=>'success'));
            exit;
        }

        $user_info = $m_users->where(array("id"=>$userid))->find();
        $this->assign("user_login",$user_info['user_login']);
        $this->assign("avatar",$user_info['avatar']);

        $list = $m_complaint->where(array("userid"=>$userid,'type'=>1))->order(array('createtime'=>'desc'))->select();
        $this->assign("list",$list);
        $this->display();
    }
    /**
     * 投诉留言
     */
    public function complaint_2(){
        $this->assign("menu_name",'complaint');
        $this->assign("title",'投诉留言');
        
        $userid = sp_get_current_userid();
        $m_users = M("Users");
        $m_complaint = M("Complaint");

        if (IS_POST) {
            $content=I('post.content/s','');
            $name=I('post.content/s','');
            $phone=I('post.content/s','');

            $save_data = array(
                'userid'=>$userid,
                'name'=>$name,
                'phone'=>$phone,
                'content'=>$content,
                'type'=>2,
            );
            $return = $m_complaint->add($save_data);
            $this->ajaxReturn(array('status'=>1,'message'=>'success'));
            exit;
        }

        $user_info = $m_users->where(array("id"=>$userid))->find();
        $this->assign("user_login",$user_info['user_login']);
        $this->assign("avatar",$user_info['avatar']);

        $list = $m_complaint->where(array("userid"=>$userid,'type'=>2))->order(array('createtime'=>'desc'))->select();
        $this->assign("list",$list);
        $this->display();
    }
    /**
     * 投诉留言
     */
    public function complaint_3(){
        $this->assign("menu_name",'complaint');
        $this->assign("title",'投诉留言');
        
        $userid = sp_get_current_userid();
        $m_users = M("Users");
        $m_complaint = M("Complaint");

        if (IS_POST) {
            $content=I('post.content/s','');
            $name=I('post.content/s','');
            $phone=I('post.content/s','');

            $save_data = array(
                'userid'=>$userid,
                'name'=>$name,
                'phone'=>$phone,
                'content'=>$content,
                'type'=>3,
            );
            $return = $m_complaint->add($save_data);
            $this->ajaxReturn(array('status'=>1,'message'=>'success'));
            exit;
        }

        $user_info = $m_users->where(array("id"=>$userid))->find();
        $this->assign("user_login",$user_info['user_login']);
        $this->assign("avatar",$user_info['avatar']);

        $list = $m_complaint->where(array("userid"=>$userid,'type'=>3))->order(array('createtime'=>'desc'))->select();
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 留言咨询
     */
    public function message(){
        $this->assign("menu_name",'message');
        $this->assign("title",'留言咨询');
        $this->display();
    }

    /**
     * 评论相关
     */
    public function comment(){
        $this->assign("menu_name",'comment');
        $this->assign("title",'商品评论');
        $this->display();
    }

    /**
     * 通知
     */
    public function notify(){
        $this->assign("menu_name",'notify');
        $this->assign("title",'系统消息');
        $this->display();
    }

    /**
     * 地址更新
     */
    public function address_save(){
        $m_address = M('Address');
        $session_user=session('user');
        $post  = array(
            'uid'=>$session_user['id'],
            'name'=>I('post.name'),
            'addr'=>I('post.addr'),
            'mobile'=>I('post.mobile'),
            'zipcode'=>I('post.zipcode'),
            'tel'=>I('post.tel'),
            'province'=>I('post.province'),
            'city'=>I('post.city'),
            'area'=>I('post.area'),
            'receiveAddrId'=>I('post.receiveAddrId'),
        );
        $receiveAddrId = I("post.receiveAddrId");
        unset($post['receiveAddrId']);
        if(empty($receiveAddrId)){
            $m_address->add($post);

        }else{
            $m_address->where(array("id"=>$receiveAddrId))->save($post);
        }
        exit;
    }

    /**
     * 获取地址
     */
    public function get_address(){
        $m_address = M('Address');
        $id = I('post.id');
        $item = $m_address->where(array('id'=>$id))->find();
        die(json_encode($item));
    }

    public function delete_address(){
        $id = I('get.id');
        if(!empty($id)){
            $m_address = M('Address');
            $m_address->where(array('id'=>$id))->delete();
        }
    }

}


